package cn.com.cifi.mars.web.action.operateplan;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import cn.com.cifi.mars.bean.param.returnPlan.ReturnPlanApprovalSubmitParam;
import cn.com.cifi.mars.bean.param.returnPlan.ReturnPlanCreateParam;
import cn.com.cifi.mars.bean.param.returnPlan.ReturnPlanDetailSaveParam;
import cn.com.cifi.mars.bean.vo.returnPlan.ReturnCreateSourceDataVo;
import cn.com.cifi.mars.bean.vo.returnPlan.ReturnPlanApprovalPreviewVo;
import cn.com.cifi.mars.bean.vo.returnPlan.ReturnPlanEditDataVo;
import cn.com.cifi.mars.bean.vo.returnPlan.ReturnPlanGroupVo;
import cn.com.cifi.mars.bean.vo.returnPlan.ReturnPlanOverviewDataVo;
import cn.com.cifi.mars.bean.vo.returnPlan.ReturnPlanReturnCoefficientDetailVo;
import cn.com.cifi.mars.bean.vo.returnPlan.ReturnPlanVersionDataVo;
import cn.com.cifi.mars.bean.vo.returnPlan.ReturnPlanYearDataVo;
import cn.com.cifi.mars.bean.vo.returnPlan.returnReport.ReturnPlanReport;
import cn.com.cifi.mars.common.base.BaseAction;
import cn.com.cifi.mars.common.bean.R;
import cn.com.cifi.mars.constant.VersionEventEnum;
import cn.com.cifi.mars.entity.PortalTodo;
import cn.com.cifi.mars.entity.common.IdmUser;
import cn.com.cifi.mars.service.BudgetVersionIdentifyService;
import cn.com.cifi.mars.service.DmMyReturnAmountService;
import cn.com.cifi.mars.service.ReturnPlanVersionReportService;
import cn.com.cifi.mars.service.ReturnPlanVersionService;
import cn.com.cifi.mars.util.OverallReturnPlanTemplate;
import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @Description：回款计划
 * @author：yyd
 * @date：2020年1月6日 上午10:49:14
 */
@Slf4j
@RestController
public class ReturnPlanController extends BaseAction implements IReturnPlanController {

    @Autowired
    private ReturnPlanVersionService returnPlanVersionService;
    @Autowired
    private ReturnPlanVersionReportService returnPlanVersionReportService;
    @Autowired
	private BudgetVersionIdentifyService budgetVersionIdentifyService;

    @Override
    public R<ReturnCreateSourceDataVo> createSourceData(String apiVersion, String stageId, String versionType) {
        ReturnCreateSourceDataVo vo = returnPlanVersionService.getCreateSourceData(stageId,versionType);
        return ok(vo);
    }

    @Override
    public R create(String apiVersion, ReturnPlanCreateParam param) {
        IdmUser user = getOperator();
        returnPlanVersionService.create(param,user);
        return ok();
    }

    @Override
    public R exceptionCreate(String apiVersion, ReturnPlanCreateParam param) {
        IdmUser user = getOperator();
        returnPlanVersionService.exceptionCreate(param,user);
        return ok();
    }

    @Override
    public R newVersionType(String apiVersion, String stageId) {
        String versionType = returnPlanVersionService.getNewVersionType(stageId);
        return ok(versionType);
    }

    @Override
    public R<List<ReturnPlanVersionDataVo>> returnPlanVersionDataList(String apiVersion, String stageId, String versionType) {
        List<ReturnPlanVersionDataVo> returnPlanDataVoList = returnPlanVersionService.getReturnPlanVersionDataList(stageId,versionType);
        return ok(returnPlanDataVoList);
    }

    @Override
    public R<List<String>> yearList(String apiVersion, String versionId) {
        List<String> list = returnPlanVersionService.getYearList(versionId);
        return ok(list);
    }

    @Override
    public R<List<ReturnPlanYearDataVo>> annualData(String apiVersion, String versionId, String year) {
        List<ReturnPlanYearDataVo> voList = returnPlanVersionService.getAnnualData(versionId,year);
        return ok(voList);
    }

    @Override
    public R<PortalTodo> getPortalTodo(String apiVersion, String combinationId) {
        PortalTodo portalTodo = returnPlanVersionService.getPortalTodo(combinationId);
        return ok(portalTodo);
    }

    @Override
    public R<String> del(String apiVersion, String versionId) {
        IdmUser user = getOperator();
        returnPlanVersionService.delById(versionId,user);
        return ok();
    }

    @Override
    public R<ReturnPlanEditDataVo> editDataList(String apiVersion, String versionId, String groupId) {
        ReturnPlanEditDataVo vo = returnPlanVersionService.getEditDataList(versionId,groupId);
        return ok(vo);
    }

    @Override
    public R<String> editSave(String apiVersion, List<ReturnPlanDetailSaveParam> paramList) {
        IdmUser user = getOperator();
        returnPlanVersionService.editSave(paramList,user);
        return ok();
    }

    @Override
    public R<List<ReturnPlanGroupVo>> groupList(String apiVersion, String versionId) {
        List<ReturnPlanGroupVo> voList = returnPlanVersionService.getGroupList(versionId);
        return ok(voList);
    }

    @Override
    public R<List<ReturnPlanOverviewDataVo>> fullOverview(String apiVersion, String versionId, String year, String month) {
        List<ReturnPlanOverviewDataVo> voList = returnPlanVersionService.fullOverview(versionId,year,month);
        return ok(voList);
    }

    @Override
    public R<List<ReturnPlanReturnCoefficientDetailVo>> returnCoefficientDetailList(String apiVersion, String versionId) {
        List<ReturnPlanReturnCoefficientDetailVo> voList = returnPlanVersionService.getReturnCoefficientDetailList(versionId);
        return ok(voList);
    }

    @Override
    public R assessment(String apiVersion, String versionId) {
        returnPlanVersionService.setAssessment(versionId);
        return ok();
    }

    @Override
    public R<List<String>> overallYearList(String apiVersion, String projectId) {
        List<String> list = returnPlanVersionService.overallYearList(projectId);
        return ok(list);
    }

    @Override
    public R<List<ReturnPlanYearDataVo>> overallAnnualData(String apiVersion, String projectId, String year) {
        List<ReturnPlanYearDataVo> voList = returnPlanVersionService.overallAnnualData(projectId,year);
        return ok(voList);
    }

    @Override
    public R approvedCheck(String apiVersion, String projectId) {
        returnPlanVersionService.approvedCheck(projectId);
        return ok();
    }

    @Override
    public R<ReturnPlanApprovalPreviewVo> approvalPreviewPC(String apiVersion, String projectId, String approvalId) {
        ReturnPlanApprovalPreviewVo vo = returnPlanVersionService.getApprovalPreview(projectId,approvalId);
        return ok(vo);
    }

    @Override
    public R<ReturnPlanApprovalPreviewVo> approvalPreviewApp(String apiVersion, String projectId, String approvalId) {
        ReturnPlanApprovalPreviewVo vo = returnPlanVersionService.getApprovalPreview(projectId,approvalId);
        return ok(vo);
    }

    @Override
    public R<String> approvalSubmit(String apiVersion, ReturnPlanApprovalSubmitParam param) {
        IdmUser user = getOperator();
        String s = returnPlanVersionService.approvalSubmit(param,user);
        return ok(s);
    }


    @Override
    public void returnPlanVersionExport(String apiVersion, HttpServletResponse response, String versionId) {
        //获取回款计划导出数据
        ReturnPlanReport returnPlanExport = returnPlanVersionReportService.getReturnPlanReportByVersionId(versionId);

        //报表导出
        OverallReturnPlanTemplate overallReturnPlanTemplate = new OverallReturnPlanTemplate(returnPlanExport.getStartAndEndTimeBO(),returnPlanExport.getReportName());
        overallReturnPlanTemplate.appendRows(returnPlanExport.getReturnExportVoList());
        try (OutputStream outputStream = response.getOutputStream()){
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode(returnPlanExport.getReportName().concat(".xlsx"), "utf-8"));
            overallReturnPlanTemplate.write(outputStream);
            outputStream.flush();
        } catch (IOException e) {
            log.error("###### errorMsg: ", e);
        }
    }

    @Override
    public void returnOverallPlanVersionExport(String apiVersion, HttpServletResponse response, String projectId) {
        //获取项目全盘回款计划导出数据
        ReturnPlanReport returnPlanExport = returnPlanVersionReportService.getReturnPlanReportByProjectId(projectId);

        //报表导出
        OverallReturnPlanTemplate overallReturnPlanTemplate = new OverallReturnPlanTemplate(returnPlanExport.getStartAndEndTimeBO(),returnPlanExport.getReportName());
        overallReturnPlanTemplate.appendRows(returnPlanExport.getReturnExportVoList());
        try (OutputStream outputStream = response.getOutputStream()){
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode(returnPlanExport.getReportName().concat(".xlsx"), "utf-8"));
            overallReturnPlanTemplate.write(outputStream);
            outputStream.flush();
        } catch (IOException e) {
            log.error("###### errorMsg: ", e);
        }
    }

    @Override
    public R updateState(String apiVersion, List<String> versionIds, String versionState) {
        IdmUser user = getOperator();
        returnPlanVersionService.updateState(versionIds, VersionEventEnum.SYS_REFRESH.getKey(),versionState,user);
        return ok();
    }

    @Override
    public R returnNoticeDispose(String apiVersion, String signId) {
        returnPlanVersionService.returnNoticeDispose(signId);
        return ok();
    }

    @Override
    public R deleteTodoInfo(String apiVersion, String versionId, String operatorType) {
        IdmUser user = getOperator();
        returnPlanVersionService.deleteTodoInfo(versionId,operatorType,user);
        return ok();
    }
    

    @Override
	public R copyBudgetVersion(String apiVersion, String versionId) {
    	IdmUser user = getOperator();
		String versionIdentify = budgetVersionIdentifyService.getVersion();
		returnPlanVersionService.copyToBudgetVersion(versionId,versionIdentify,user);
		return ok();
	}


	@Override
	public R copyReturnPlanVersion(String apiVersion, String versionId) {
		IdmUser user = getOperator();
		String versionIdentify = budgetVersionIdentifyService.getVersion();
		returnPlanVersionService.copyBudgetVersion(versionId,versionIdentify,user);
		return ok();
	}




	@Autowired
    private DmMyReturnAmountService dmMyReturnAmountService;
    @Override
    public R syncMyReturnData(String apiVersion, String stageId) {
        dmMyReturnAmountService.syncMyReturnData();
        return ok();
    }
    
}
